// miniprogram/pages/logs/logs.js
const db = wx.cloud.database()
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    array: ['私密', '公开', '喜欢'],
    typeIndex: "0"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (!app.globalData.openid) {
      return false
    }
    this.getList()
  },
  detail: function (e) {
    const { id } = e.currentTarget
    wx.navigateTo({
      url: '../detail/detail?id=' + id,
    })
  },
  detele: function (e) {
    const that = this
    const { list } = this.data
    const deteleItem = list.find(item => item._id === e.target.id)
    console.log(deteleItem)
    delete deteleItem._openid

    wx.showModal({
      content: '刪除?',
      success (res) {
        if (res.confirm) {
          db.collection('bin').add({
            // data 字段表示需新增的 JSON 数据
            data: deteleItem
          })
          db.collection('logs').doc(e.target.id).remove()
            .then(() => {
              wx.showToast({
                title: '删除成功',
                icon: "none"
              })
              that.onShow()
            })
            .catch(console.error)
        } 
      }
    })

  },
  down:function (e) {
    const that = this
    const { list } = this.data
    const deteleItem = list.find(item => item._id === e.target.id)
    console.log(e)
    console.log(list,deteleItem)
    delete deteleItem._openid
    wx.showModal({
      content: '私密？',
      success (res) {
        if (res.confirm) {
          db.collection('bin').add({
            // data 字段表示需新增的 JSON 数据
            data: deteleItem
          })
          db.collection('logs').doc(e.target.id).update({
            data: {
              open: false
            },
          })
          .then(() => {
            wx.showToast({
              title: '私密成功',
              icon: "none"
            })
            that.onShow()
          })
        } 
      }
    })

  },
  bindPickerChange: function (e) {
    this.setData({
      typeIndex: e.detail.value
    })
    this.getList()
  },
  getList: function () {
    const that = this
    const { typeIndex } = this.data
    switch (typeIndex) {
      case "0":
        db.collection('logs').where({
          _openid: app.globalData.openid,
          open: false
        }).get().then(res => {
          const list = res.data
          that.setData({ list })
        })
        break;
      case "1":
        db.collection('logs').where({
          _openid: app.globalData.openid,
          open: true
        }).get().then(res => {
          const list = res.data
          that.setData({ list })
        })
        break;
      default:
        db.collection('likes').where({
          type: true,
          _openid: app.globalData.openid,
        }).get().then(res => {
          console.log(res.data)
          const ids = res.data.map(item => item.logId)
          console.log(ids)
          const _ = db.command
          db.collection('logs').where({
            _id: _.in(ids)// 填入当前用户 openid
          }).get().then(res => {
            const list = res.data
            that.setData({ list })
          })
        })
    }
  }
})